<template>
    <div>
        <div>
            <span>会员号：</span>
            <el-input v-if="!this.$route.query.userNum" placeholder="请输入会员号" v-model="userNum" class="input">
                <el-button @click="search"  slot="append" icon="el-icon-search" type="primary" plain></el-button>
            </el-input>
            <el-input v-else="!this.$route.query.userNum" readonly placeholder="请输入会员号" v-model="userNum" class="input"/>
            <span v-if="msg">{{msg}}
                <el-button type="primary" @click="toVipInfo">添加会员</el-button>
            </span>

        </div>
        <div>
            <br>
            <div>
                <el-row style="text-align: left">
                    <el-col :span="1">
                        <p></p>
                    </el-col>
                    <el-col :span="3">
                        <div class="grid-content bg-purple"></div>
                        <el-button type="primary" :disabled="userId == null" @click="onhandAdd">添加一件衣物</el-button>
                    </el-col>
                    <el-col :span="3">
                        <div class="grid-content bg-purple"></div>
                        <el-button type="primary" :disabled="userId == null" @click="choice">从我的衣物中选择</el-button>
                    </el-col>

                </el-row>
            </div>
            <br>
            <el-row>
                <el-col :span="18">
                    <el-card>
                        <el-table :data="tableData" stripe style="width: 100%">
                            <el-table-column
                                    type="index"
                                    width="50">
                            </el-table-column>
                            <el-table-column prop="clothesMaterial" label="衣物材质" width="180">
                                <template v-slot="scope">
                                    <el-select ref="selectclothesMaterial" filterable
                                               v-model="scope.row.clothesMaterial" :disabled="scope.row.ishad"
                                               value-key="clothesMaterialId"
                                    >
                                        <el-option
                                                v-for="item in allClothesMaterial"
                                                :key="item.clothesMaterialId"
                                                :label="item.clothesMaterialName"
                                                :value="item"
                                        >
                                        </el-option>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="clothesMaterial.washTime"
                                    label="洗衣时间/天"
                                    width="180">
                            </el-table-column>
                            <el-table-column prop="clothesKind" label="衣物种类" width="180">
                                <template v-slot="scope">
                                    <el-select ref="selectclothesKind" filterable v-model="scope.row.clothesKind"
                                               value-key="clothesKindId"
                                               :disabled="scope.row.ishad"
                                               @change="saveMaterial()">
                                        <el-option
                                                v-for="item in allClothesKind"
                                                :key="item.clothesKindId"
                                                :label="item.clothesKindName"
                                                :value="item"
                                        >
                                        </el-option>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="clothesKind.clothesPrice"
                                    label="价格"
                                    width="180">
                            </el-table-column>
                            <el-table-column label="操作" prop="washKindId">
                                <template v-slot="scope">
                                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info"
                                                   icon-color="red"
                                                   title="确认删除此件衣物吗？" @confirm="del(scope.$index),saveMaterial()">
                                        <el-button type="danger" slot="reference">删除</el-button>
                                    </el-popconfirm>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-card>

                    <el-card  style="margin-top: 20px">
                        <div style="float: right;margin-right:100px">
                            <span>总价格：￥{{total}}</span>
                            <br>
                            <span>超会折扣：{{zhekoulv}}</span>
                            <hr style="width: 10vw">
                            <span>合计：￥{{sum}}</span>

                            <br>
                            <br>
                            <el-button type="success" @click="submit">提交订单</el-button>
                        </div>
                    </el-card>
                </el-col>

            </el-row>
        </div>
        <!--        选择已有衣服的弹框-->
        <el-dialog title="已有衣物" el-dialog :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-table ref="multipleTable" :data="clothes" stripe style="width: 100%" tooltip-effect="dark"
                      @selection-change="handleSelectionChange">
                <el-table-column
                        prop="clothesKindId"
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        type="index"
                        width="50">
                </el-table-column>
                <el-table-column property="clothesKind.clothesKindName" label="衣物种类" width="250"></el-table-column>
                <el-table-column property="clothesMaterial.clothesMaterialName" label="衣物材质"
                                 width="200"></el-table-column>

            </el-table>
            <div style="text-align-last: center;width: 60%;">
                <el-button @click="closedialog" type="danger">取 消</el-button>
                <el-button @click="toggleSelection()">取消选择</el-button>
                <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                           @click="dialogVisibleConfirm" size="medium">添加
                </el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import request from "@/request";

    export default {
        name: "AddOrder",
        data(){
            return{
                userId :this.$route.query.userId,
                userNum: this.$route.query.userNum,
                tableData: [],
                allClothesMaterial: [],
                allClothesKind: [],
                total: "",
                zhekoulv: '',
                sum: '',
                dialogVisible: false,
                clothes: [],
                multipleSelection: [],
                disable:true,
                msg:"",
            }
        },
        methods:{
            //根据会员号获取信息
            search(){
                request.get('/user/searchByuserNum/'+this.userNum).then(res => {
                    if (res.code === "200") {
                        this.userId = res.data
                        this.$notify.success(res.msg)
                        this.msg = null
                        this.getZheKouLv()
                    }else {
                        this.msg = res.msg
                    }
                })
            },
            //前往会员信息页
            toVipInfo(){
                this.$router.push("/receptionadmin/vipinfo")
            },
            onhandAdd() {
                this.tableData.push({ishad: false})
            },
            //获取所有的衣物材质
            getAllClothesMaterial() {
                request.get('/clothesmaterial/getall').then(res => {
                    console.log(res)
                    if (res.code === "200") {
                        this.allClothesMaterial = res.data;
                    }
                })
            },
            //获取所有衣物种类
            getAllClothesKind() {
                request.get('clotheskind/getall').then(res => {
                    if (res.code === "200") {
                        this.allClothesKind = res.data;
                    }
                })
            },
            //获取此用户超会的折扣率
            getZheKouLv() {
                console.log(this.userId)
                request.get('vipcard/getvipcard/' + this.userId).then(res => {
                    console.log(res)
                    if (res.code === "200") {
                        if (!res.data) {
                            this.$message.error("未拥有超级会员！")
                            this.zhekoulv = 1
                        } else {
                            this.zhekoulv = res.data.vipKindZhekoulv
                        }
                    }
                })
            },
            saveMaterial() {
                console.log(this.tableData);
                this.total = 0
                for (let i = 0; i < this.tableData.length; i++) {
                    this.total += this.tableData[i].clothesKind.clothesPrice
                }
                this.sum = this.total * this.zhekoulv
                this.sum = (Number(this.sum)).toFixed(2)
            },
            del(index) {
                this.tableData.splice(index, 1)
            },
            choice() {
                request.get('clothes/getclothesbyuserid/' + this.userId).then(res => {
                    if (res.code === "200") {
                        this.clothes = res.data
                    }
                })
                this.dialogVisible = true
            },
            handleClose(done) {
                this.dialogVisible = false;
            },
            closedialog() {
                this.dialogVisible = false;
                this.multipleSelection.length=0 //清空已选id
            },
            //更改多选框时的操作
            handleSelectionChange(val) {
                //如果什么都没有选中则禁用删除按钮
                if (val.length==0){
                    this.multipleSelection.length=val.length
                    this.disable=true
                }else {
                    this.disable=false
                    //将选中的id存放全局
                    this.multipleSelection.length=val.length
                    for (let i = 0; i < val.length ;i++) {
                        this.multipleSelection[i]=val[i]
                    }
                }
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            dialogVisibleConfirm(){
                this.multipleSelection.forEach(function (item) {
                    item.ishad=true;
                })
                this.tableData.push(...this.multipleSelection)
                this.saveMaterial()
                this.dialogVisible=false
            },
            submit() {
                console.log(this.tableData);
                request.post("/order/submit/"+this.userId+"/"+this.total+"/"+this.sum,this.tableData).then(res => {
                    if (res.code === "200") {
                        this.$notify.success("创建订单成功，请前往查看")
                        this.$router.push("/receptionadmin/vieworder")
                    }else {
                        this.$message.error(res.msg)
                    }
                })
            },
        },
        mounted() {
            this.getAllClothesMaterial();
            this.getAllClothesKind();
        },
        created() {
            this.getZheKouLv()
        }
    }
</script>

<style scoped>
.input{
    width: 15vw;
}
</style>